package com.techsoul.module.accounting.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ExcelException;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.core.util.poi.ExcelExport;
import com.techsoul.common.core.util.poi.ExcelImport;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.accounting.mapper.MaterialBuyPriceMapper;
import com.techsoul.module.accounting.pojo.entity.MaterialBuyPrice;
import com.techsoul.module.accounting.pojo.param.MaterialBuyPriceSaveParam;
import com.techsoul.module.accounting.pojo.param.MaterialBuyPriceSearchParam;
import com.techsoul.module.accounting.pojo.param.MaterialSalesPriceSaveParam;
import com.techsoul.module.accounting.pojo.vo.MaterialBuyPriceVo;
import com.techsoul.module.accounting.service.IMaterialBuyPriceService;
import com.techsoul.module.accounting.service.IMaterialSalesPriceService;
import com.techsoul.module.basic.pojo.vo.StoreVo;
import com.techsoul.module.basic.service.IStoreService;
import com.techsoul.module.goods.enmus.SpuStatusEnum;
import com.techsoul.module.goods.mapper.BrandMapper;
import com.techsoul.module.goods.mapper.SpuMapper;
import com.techsoul.module.goods.pojo.entity.Brand;
import com.techsoul.module.goods.pojo.entity.Spu;
import com.techsoul.module.goods.pojo.vo.MaterialImportVo;
import com.techsoul.module.goods.service.ISpuService;
import com.techsoul.module.pms.mapper.CustGoodsMapMapper;
import com.techsoul.module.pms.mapper.GoodsGroupMapper;
import com.techsoul.module.pms.mapper.GoodsSupplierMapper;
import com.techsoul.module.goods.mapper.SkuMapper;
import com.techsoul.module.pms.pojo.entity.CustGoodsMap;
import com.techsoul.module.pms.pojo.entity.GoodsGroup;
import com.techsoul.module.pms.pojo.entity.GoodsSupplier;
import com.techsoul.module.goods.pojo.entity.Sku;
import com.techsoul.module.pms.pojo.param.CustGoodsMapSaveParam;
import com.techsoul.module.pms.pojo.vo.CustGoodsMapVo;
import com.techsoul.module.pms.pojo.vo.GoodsSupplierVo;
import com.techsoul.module.pms.service.ICustGoodsMapService;
import com.techsoul.module.pms.service.IGoodsSupplierService;
import com.techsoul.module.system.enmus.ErrorType;
import com.techsoul.module.system.mapper.OperationLogMapper;
import com.techsoul.module.system.pojo.entity.OperationLog;
import com.techsoul.module.system.service.IConfigService;
import com.techsoul.module.system.service.IGeneralSeqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 供应商采购价格配置基础，用于配置价格，产生最后付款结算。 Service实现
 *
 * @author liuen
 * @date 2021-04-11
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class MaterialBuyPriceServiceImpl implements IMaterialBuyPriceService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MaterialBuyPriceMapper mapper;

    @Autowired
    private CustGoodsMapMapper custGoodsMapMapper;

    @Autowired
    private IStoreService iStoreService;

    @Autowired
    private IMaterialSalesPriceService iMaterialSalesPriceService;

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Autowired
    private GoodsSupplierMapper goodsSupplierMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuMapper skuMapper;


    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private GoodsGroupMapper goodsGroupMapper;

    @Autowired
    private IGoodsSupplierService iGoodsSupplierService;

    @Autowired
    private ISpuService iSpuService;

    @Autowired
    private ICustGoodsMapService iCustGoodsMapService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IConfigService configService;

    @Autowired
    private IGeneralSeqService generalSeqService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(MaterialBuyPriceSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        MaterialBuyPrice entity = new MaterialBuyPrice();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(MaterialBuyPriceSaveParam params) {
        MaterialBuyPrice entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public MaterialBuyPriceVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<MaterialBuyPriceVo> findAllList(MaterialBuyPriceSearchParam params) {
        return this.mapper.findAllList(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<MaterialBuyPriceVo> findAllList(MaterialBuyPriceSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAllList(new Page<>(current, pageSize), params);
    }
    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<MaterialBuyPriceVo> findAll(MaterialBuyPriceSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public List<MaterialBuyPriceVo> getByMapId(String id)    {
        return this.mapper.getByMapId(id);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<MaterialBuyPriceVo> findAll(MaterialBuyPriceSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

    /**
     * 导入模板
     */
    @Override
    public String importTemplate() {
        String baseDir = configService.findByConfigKey("project.attachment.baseDir");
        String baseUrl = configService.findByConfigKey("project.attachment.baseUrl");
        String fileName = "导入模板-供应商-商品信息和价格.xlsx";
        try {
            new ExcelExport(null, MaterialBuyPriceVo.class).writeFile(baseDir + fileName).close();
        } catch (Exception e) {
            log.error("生成供应商采购价格导入模板失败", e);
            throw new ExcelException(e.getMessage());
        }
        return baseUrl + fileName;
    }

    /**
     * 生成商品组合
     */
    @Override
    public GoodsGroup generateNewGoodsGroup(MaterialBuyPriceVo materialBuyPriceVo){
        GoodsGroup spu = new GoodsGroup();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setGoodsGroupName(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuName(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuId(materialBuyPriceVo.getSpuId());
        spu.setId(idWorker.nextId() + "").preInsert(user.getId());
        goodsGroupMapper.insert(spu);
        return spu;
    }
    /**
     * 生成供应商商品链表
     */
    @Override
    public GoodsSupplier generateNewGoodsSupplier(MaterialBuyPriceVo materialBuyPriceVo,String supplierNo,String supplierName,String spuid,String storeId,String storeName){
        GoodsSupplier spu = new GoodsSupplier();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setSupplierId(supplierNo);
        if(materialBuyPriceVo.getGroupOrSpu().equals("单品")){
            spu.setGoodsType("spu");
        }else if(materialBuyPriceVo.getGroupOrSpu().equals("组合")){
            spu.setGoodsType("gp");
        }else{
            spu.setGoodsType("sku");
        }
        spu.setSkuName(materialBuyPriceVo.getCustGoodsName());
        spu.setSn(materialBuyPriceVo.getCustGoodsNo());
        spu.setStoreId(storeId);
        spu.setStoreName(storeName);
        spu.setSpuId(spuid);
        spu.setSupplierName(supplierName);
        spu.setWarehouse(materialBuyPriceVo.getWarehouse());
        spu.setId(idWorker.nextId() + "").preInsert(user.getId());
        goodsSupplierMapper.insert(spu);
        return spu;
    }

    /**
     * 生成商品SKU
     */
    @Override
    public Sku generateNewSku(MaterialBuyPriceVo materialBuyPriceVo,Spu spu){
        Sku sku = new Sku();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        sku.setBrandId(materialBuyPriceVo.getBrandId());
        sku.setBrandName(materialBuyPriceVo.getBrandName());
        sku.setCategoryId(materialBuyPriceVo.getCategoryId());
        sku.setSpuId(spu.getId());
        sku.setCustSkuId(materialBuyPriceVo.getCustGoodsNo());
        sku.setCategoryName(materialBuyPriceVo.getCategoryName());
        sku.setSn(materialBuyPriceVo.getCustGoodsNo());
        sku.setImage(materialBuyPriceVo.getImage());
        sku.setSpec(materialBuyPriceVo.getMaterialGuige());
        sku.setSkuName(materialBuyPriceVo.getCustGoodsName());
        sku.setStatus(SpuStatusEnum.CREATED.getStatus());
        sku.setIsValid("1");
        String skuid =   this.generalSeqService.generateSeq("sku");
        sku.setId(skuid);
        sku.preInsert(user.getId());
        skuMapper.insert(sku);
        return sku;
    }
    /**
     * 生成商品
     */
    @Override
    public Spu generateNewSpu(MaterialBuyPriceVo materialBuyPriceVo,StoreVo storeVo,String custSpuNO){
        Spu spu = new Spu();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(!StringUtils.isNull(materialBuyPriceVo.getBrandId())){
            Brand brand = brandMapper.selectById(materialBuyPriceVo.getBrandId());
            spu.setBrandId(brand.getId());
            spu.setBrandName(brand.getBrandCode());
        }else if(!StringUtils.isNull(materialBuyPriceVo.getBrandName())){
            Brand brand = brandMapper.findByName(materialBuyPriceVo.getBrandName());
            if(brand==null) {
                brand = new Brand();
                brand.setBrandCode(materialBuyPriceVo.getBrandCode());
                if(!StringUtils.isNull(materialBuyPriceVo.getBrandName())){
                    brand.setBrandName(materialBuyPriceVo.getBrandName());
                }else{
                    brand.setBrandName(materialBuyPriceVo.getBrandCode());
                }

                brand.setStoreId(storeVo.getId());
                brand.setId(idWorker.nextId() + "").preInsert(user.getId());
                brandMapper.insert(brand);
            }
            spu.setBrandId(brand.getId());
            spu.setBrandName(materialBuyPriceVo.getBrandName());
        }else if(!StringUtils.isNull(materialBuyPriceVo.getBrandCode())){
            Brand brand = brandMapper.findByCode(materialBuyPriceVo.getBrandCode());
            if(brand==null){
                brand = new Brand();
                brand.setBrandCode(materialBuyPriceVo.getBrandCode());
                if(!StringUtils.isNull(materialBuyPriceVo.getBrandName())){
                    brand.setBrandName(materialBuyPriceVo.getBrandName());
                }else{
                    brand.setBrandName(materialBuyPriceVo.getBrandCode());
                }
                brand.setStoreId(storeVo.getId());
                brand.setId(idWorker.nextId() + "").preInsert(user.getId());
                brandMapper.insert(brand);

            }
            spu.setBrandId(brand.getId());
            spu.setBrandName(materialBuyPriceVo.getBrandCode());

        }
        spu.setCategoryId(materialBuyPriceVo.getCategoryId());
        spu.setSn(materialBuyPriceVo.getCustGoodsNo());
        spu.setTitle(materialBuyPriceVo.getCustGoodsName());
        spu.setCategoryName(materialBuyPriceVo.getCategoryName());
        spu.setImage(materialBuyPriceVo.getImage());
        spu.setIntroduction(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuName(materialBuyPriceVo.getCustGoodsName());
        spu.setStatus(SpuStatusEnum.CREATED.getStatus());
        spu.setSpecs(materialBuyPriceVo.getMaterialGuige());
        spu.setCustSpuNo(custSpuNO);

        String spuid =   this.generalSeqService.generateSeq("spu");
        spu.setId(spuid);
        spu.preInsert(user.getId());
        spu.setSite(storeVo.getMallId());
        spuMapper.insert(spu);
        return spu;
    }
    /**
     * 导入
     * @param file
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String importData(String storeId, Date applyDate, MultipartFile file) {
        List<MaterialBuyPriceVo> dataList = null;
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        StoreVo storeVo = iStoreService.findById(storeId);
        String supplierNo = storeVo.getSupplierId();
        String supplierName = storeVo.getSupplierName();
        int successNum = 0;
        int failureNum = 0;
        int logNum = 0;
        try {
            ExcelImport excelImport = new ExcelImport(file, 1, 0);
            dataList = excelImport.getDataList(MaterialBuyPriceVo.class);
        } catch (Exception e) {
            log.error("获取数据信息失败!");
            throw new ExcelException("获取数据信息失败!");
        }
        if (ListUtils.isEmpty(dataList)) {
            throw new ExcelException("导入数据不能为空！");
        }
        Spu newspu = new Spu();
        List<String> scaSpuNos = new ArrayList<String>();
        int line = 0;
        for (MaterialBuyPriceVo data : dataList) {
            data = this.removeSpecialCharMaterialBuyPriceVo(data);
            line++;
            String custGoodNo = data.getCsaSpuCode();
            String addSpuYn="Y";
            if(StringUtils.isNull(data.getCustRelSalesPrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.BUYPRICEIMPORT.getStatusName());
                operationLog.setErrorMsg("在销售费率表相应得销售费率没有找到");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-供应商-商品信息和价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);//add log
                logNum++;
                continue;
            }else{
                if(!StringUtils.isDouble(data.getCustRelSalesPrice())){
                    //add log
                    OperationLog operationLog =new OperationLog();
                    operationLog.setOperationType(ErrorType.BUYPRICEIMPORT.getStatusName());
                    operationLog.setErrorMsg("相应得销售费率格式不对");
                    operationLog.setRequestMethod(custGoodNo);
                    operationLog.setLocation("第"+line+"行");
                    operationLog.setMethod("导入模板-供应商-商品信息和价格");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);//add log
                    logNum++;
                    continue;
                }
            }
            if(StringUtils.isNull(data.getSettlePrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.BUYPRICEIMPORT.getStatusName());
                operationLog.setErrorMsg("采购费率没有提供");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-供应商-商品信息和价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
                logNum++;
                continue;
            }
            if(scaSpuNos.contains(custGoodNo)){
                List<Spu> spus =  this.spuMapper.findByCustSpuNo(custGoodNo,storeVo.getMallId());
                if(spus!=null&&spus.size()>0){
                    newspu =(Spu)spus.get(0);
                    addSpuYn = "N";
                }
            }else{
                scaSpuNos.add(custGoodNo);
            }

            // add more data check log
            try {
                MaterialSalesPriceSaveParam saleParam = new MaterialSalesPriceSaveParam();
                MaterialBuyPriceSaveParam saveParam = new MaterialBuyPriceSaveParam();
                BeanUtils.copyProperties(data, saveParam);

                if(StringUtils.isNull(data.getCsaSkuCode())) { // 导入数据没有SKU
                    CustGoodsMapVo cgm = custGoodsMapMapper.findByCustGoodNoAndSkuNo(custGoodNo, "通用", storeVo.getMallId());
                    if(cgm==null){
                        CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        custGoodsMapSaveParam.setMallId(storeVo.getMallId());
                   /*     if(data.getGroupOrSpu().equals("组合")){
                            custGoodsMapSaveParam.setGoodsType("gp");
                            GoodsGroup newGoodsGroup = this.generateNewGoodsGroup(data);
                            custGoodsMapSaveParam.setSpuId(newGoodsGroup.getId());
                            custGoodsMapSaveParam.setGroupId(newGoodsGroup.getId());
                            this.generateNewGoodsSupplier(data,supplierNo,supplierName,newGoodsGroup.getId(),storeVo.getId(),storeVo.getStoreName());

                            saveParam.setGroupOrSpu("gp");
                            saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saveParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode("通用");
                            saveParam.setCustGoodsName(data.getCustGoodsName());
                            saleParam.setGroupOrSpu("gp");
                            saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saleParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saleParam.setCustomerNo(storeVo.getMallId());

                        }else{*/
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setGoodsType("spu");
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(data,storeVo,custGoodNo);
                        }

                        custGoodsMapSaveParam.setBrandId(newspu.getBrandId());
                        custGoodsMapSaveParam.setBrandName(newspu.getBrandName());
                        custGoodsMapSaveParam.setStoreId(storeVo.getId());
                        custGoodsMapSaveParam.setSpuName(newspu.getSpuName());
                        // Sku newsku = this.generateNewSku(data,newspu);
                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        custGoodsMapSaveParam.setBarCode(data.getBarCode());
                        custGoodsMapSaveParam.setSn(data.getCustGoodsNo());
                        // custGoodsMapSaveParam.setSkuId(newsku.getId());
                        //  String cgmid =iCustGoodsMapService.insert(custGoodsMapSaveParam);
                        GoodsSupplier goodsSupplier =
                                this.generateNewGoodsSupplier(data,supplierNo,supplierName,newspu.getId(),storeVo.getId(),storeVo.getStoreName());

                        saveParam.setGroupOrSpu("spu");
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);
                        saveParam.setCsaSpuCode(data.getCsaSpuCode());
                        saveParam.setCsaSkuCode("通用");
                        saveParam.setCustGoodsName(data.getCustGoodsName());
                        saleParam.setGroupOrSpu("spu");
                        saveParam.setBrandName(custGoodsMapSaveParam.getBrandName());
                        saveParam.setBarCode(data.getBarCode());
                        saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saleParam.setCustomerNo(storeVo.getMallId());
                        saleParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        //     }
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        //entity.setId(idWorker.nextId() + "").preInsert(user.getId());
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());
                        saleParam.setMaterialMapId(entity.getId());

                    }else{ // 不是null
                        GoodsSupplierVo goodsSupplierVo= null;
                        List<GoodsSupplierVo> goods = goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,cgm.getSpuId());
                        if(goods!=null&&goods.size()>0){
                            goodsSupplierVo = (GoodsSupplierVo)goods.get(0);
                        }
                        if(goodsSupplierVo==null){
                            GoodsSupplier goodsSupplierSaveParam=  this.generateNewGoodsSupplier(data,supplierNo,supplierName,cgm.getSpuId(),storeVo.getId(),storeVo.getStoreName());
                            //   if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode(cgm.getCustSkuNo());
                            saveParam.setBrandName(cgm.getBrandName());
                            saveParam.setBarCode(data.getBarCode());
                            saleParam.setGroupOrSpu("spu");
                            saleParam.setSpuId(cgm.getSpuId());
                            saleParam.setCustomerNo(storeVo.getMallId());
                            saleParam.setSkuId(cgm.getSkuId());

                         /*   }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setGoodGroupId(cgm.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());
                                saleParam.setGroupOrSpu("gp");
                                saleParam.setSpuId(cgm.getSpuId());
                                saleParam.setSkuId(cgm.getSkuId());
                                saleParam.setGoodGroupId(cgm.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());

                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());
                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(cgm.getSkuId());
                                saleParam.setSpuId(cgm.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                            saveParam.setMaterialMapId(cgm.getId());
                            saleParam.setMaterialMapId(cgm.getId());
                        }else{  // goodsupplier not null
                            saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                            //  if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(cgm.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(cgm.getSkuId());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode(cgm.getCustSkuNo());
                            saleParam.setSkuId(cgm.getSkuId());
                            saveParam.setBrandName(cgm.getBrandName());
                            saveParam.setBarCode(data.getBarCode());
                            saleParam.setGroupOrSpu("spu");
                            saleParam.setSpuId(cgm.getSpuId());
                            saleParam.setCustomerNo(storeVo.getMallId());

                        /*    }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setGoodGroupId(cgm.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setCsaSkuCode(cgm.getCustSkuNo());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saleParam.setSkuId(cgm.getSkuId());

                                saleParam.setGroupOrSpu("gp");
                                saleParam.setSpuId(cgm.getSpuId());
                                saleParam.setGoodGroupId(cgm.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(cgm.getSkuId());
                                saveParam.setSpuId(cgm.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(cgm.getSkuId());
                                saleParam.setSpuId(cgm.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                            saveParam.setMaterialMapId(cgm.getId());
                            saleParam.setMaterialMapId(cgm.getId());
                        }
                    }
                }else{ // 导入有SKU
                    CustGoodsMapVo custGoodsMapVo = custGoodsMapMapper.findByCustGoodNoAndSkuNo(custGoodNo,data.getCsaSkuCode(),storeVo.getMallId());
                    if(custGoodsMapVo==null) {
                        CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setMallId(storeVo.getMallId());
                        custGoodsMapSaveParam.setSn(data.getCustGoodsNo());

                        custGoodsMapSaveParam.setCustSkuNo(data.getCsaSkuCode());
                     /*   if (data.getGroupOrSpu().equals("组合")) {
                            custGoodsMapSaveParam.setGoodsType("gp");
                            GoodsGroup newGoodsGroup = this.generateNewGoodsGroup(data);
                            custGoodsMapSaveParam.setSpuId(newGoodsGroup.getId());
                            custGoodsMapSaveParam.setGroupId(newGoodsGroup.getId());
                            this.generateNewGoodsSupplier(data, supplierNo, supplierName, newGoodsGroup.getId(),storeVo.getId(),storeVo.getStoreName());

                            saveParam.setGroupOrSpu("gp");
                            saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saveParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode(data.getCsaSkuCode());
                            saleParam.setGroupOrSpu("gp");
                            saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saleParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saleParam.setCustomerNo(storeVo.getMallId());
                        } else {*/
                        custGoodsMapSaveParam.setGoodsType("sku");
                        custGoodsMapSaveParam.setCustSkuNo(data.getCsaSkuCode());
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(data,storeVo,custGoodNo);
                        }

                        Sku newsku = this.generateNewSku(data, newspu);
                        custGoodsMapSaveParam.setBrandId(newspu.getBrandId());
                        custGoodsMapSaveParam.setStoreId(storeVo.getId());
                        custGoodsMapSaveParam.setSpuName(newspu.getSpuName());
                        custGoodsMapSaveParam.setSkuId(newsku.getId());
                        custGoodsMapSaveParam.setBrandName(newsku.getBrandName());
                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        custGoodsMapSaveParam.setBarCode(data.getBarCode());
                        this.generateNewGoodsSupplier(data, supplierNo, supplierName, newsku.getId(),storeVo.getId(),storeVo.getStoreName());
                        saveParam.setGroupOrSpu("sku");
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);
                        saveParam.setCsaSpuCode(data.getCsaSpuCode());
                        saveParam.setBrandName(custGoodsMapSaveParam.getBrandName());
                        saveParam.setBarCode(data.getBarCode());
                        saveParam.setCsaSkuCode(data.getCsaSkuCode());
                        saleParam.setGroupOrSpu("sku");
                        saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saleParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saleParam.setCustomerNo(storeVo.getMallId());
                        //  }
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                       // entity.setId(idWorker.nextId() + "").preInsert(user.getId());
                        String mapid =   this.generalSeqService.generateSeq("custMap");
                        entity.setId(mapid + "").preInsert(user.getId());

                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());
                        saleParam.setMaterialMapId(entity.getId());
                    }
                    else{//  不是null

                        GoodsSupplierVo goodsSupplierVo= null;
                        List<GoodsSupplierVo> goods = goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,custGoodsMapVo.getSpuId());
                        if(goods!=null&&goods.size()>0){
                            goodsSupplierVo = (GoodsSupplierVo)goods.get(0);
                        }
                        if(goodsSupplierVo==null){
                            GoodsSupplier goodsSupplierSaveParam=  this.generateNewGoodsSupplier(data,supplierNo,supplierName,custGoodsMapVo.getSpuId(),storeVo.getId(),storeVo.getStoreName());
                            //  if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(custGoodsMapVo.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(custGoodsMapVo.getSkuId());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode(data.getCsaSkuCode());
                            saveParam.setBrandName(custGoodsMapVo.getBrandName());
                            saveParam.setBarCode(data.getBarCode());
                            saleParam.setGroupOrSpu("spu");
                            saleParam.setSpuId(custGoodsMapVo.getSpuId());
                            saleParam.setCustomerNo(storeVo.getMallId());
                            saleParam.setSkuId(custGoodsMapVo.getSkuId());

                         /*   }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saleParam.setGroupOrSpu("gp");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());

                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                            saveParam.setMaterialMapId(custGoodsMapVo.getId());
                            saleParam.setMaterialMapId(custGoodsMapVo.getId());
                        }else{
                            saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                            //   if(data.getGroupOrSpu().equals("单品")){
                            saveParam.setGroupOrSpu("spu");
                            saveParam.setSpuId(custGoodsMapVo.getSpuId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setSkuId(custGoodsMapVo.getSkuId());
                            saveParam.setBrandName(custGoodsMapVo.getBrandName());
                            saveParam.setBarCode(data.getBarCode());
                            saleParam.setSkuId(custGoodsMapVo.getSkuId());
                            saleParam.setGroupOrSpu("spu");
                            saleParam.setSpuId(custGoodsMapVo.getSpuId());
                            saleParam.setCustomerNo(storeVo.getMallId());
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode(data.getCsaSkuCode());
                         /*   }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGroupOrSpu("gp");
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());

                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(data.getCsaSkuCode());
                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                            saveParam.setMaterialMapId(custGoodsMapVo.getId());
                            saleParam.setMaterialMapId(custGoodsMapVo.getId());
                        }
                    }

                }

                //    CustGoodsMapVo cgm = custGoodsMapMapper.findByCustGoodNo(custGoodNo,storeVo.getMallId());

                saleParam.setApplyDate(applyDate);
                saleParam.setSettlePrice(new BigDecimal(Double.valueOf(data.getCustRelSalesPrice())));


                saleParam.setPriceDefType("$");
                saveParam.setApplyDate(applyDate);
                saveParam.setStoreId(storeId);
                saveParam.setMallCustNo(storeVo.getMallId());
                saleParam.setStoreId(storeId);
                saveParam.setSettlePrice(new BigDecimal(Double.valueOf(data.getSettlePrice())));
                if(data.getWholeSaleQuantity()==null){
                    saveParam.setWholeSaleQuantity(new Long(0));
                }else{
                    saveParam.setWholeSaleQuantity(data.getWholeSaleQuantity());
                }
                saveParam.setGroupOrSpu(data.getGroupOrSpu());
                saveParam.setCustGoodsName(data.getCustGoodsName());
                this.insert(saveParam);
                iMaterialSalesPriceService.insert(saleParam);
                successNum++;
                successMsg.append("<br/>" + successNum + "导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "导入失败：";
                failureMsg.append(msg + e.getMessage());
                //add log table
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.BUYPRICEIMPORT.getStatusName());
                operationLog.setErrorMsg(failureMsg.toString());
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-供应商-商品信息和价格");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            OperationLog operationLog =new OperationLog();
            operationLog.setOperationType(ErrorType.BUYPRICEIMPORT.getStatusName());
            operationLog.setErrorMsg(failureMsg.toString());
            operationLog.setLocation("第"+line+"行");
            operationLog.setMethod("导入模板-供应商-商品信息和价格");
            operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
            if (user != null) {
                operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
            }
            operationLogMapper.insert(operationLog);
            throw new ExcelException(failureMsg.toString());
        } else {
            if(logNum>0){
                successMsg.insert(0, "恭喜您，操作成功！共 " + successNum + " 条，另有：" +logNum+ " 条数据不能正常导入，已写入日志");

            }else{
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");

            }
        }
        return successMsg.toString();
    }
    /**
     * 导入模板
     */
    @Override
    public String importExistSpuTemplate() {
        String baseDir = configService.findByConfigKey("project.attachment.baseDir");
        String baseUrl = configService.findByConfigKey("project.attachment.baseUrl");
        String fileName = "导入模板-商城商品信息和价格-初始化数据.xlsx";
        try {
            new ExcelExport(null, MaterialImportVo.class).writeFile(baseDir + fileName).close();
        } catch (Exception e) {
            log.error("历史数据导入模板失败", e);
            throw new ExcelException(e.getMessage());
        }
        return baseUrl + fileName;
    }
    /**
     * 导入
     * @param file
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String importExistSpuData(String mallId,MultipartFile file) {
        List<MaterialImportVo> dataList = null;
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        int successNum = 0;
        int failureNum = 0;
        int logNum = 0;
        try {
            ExcelImport excelImport = new ExcelImport(file, 1, 0);
            dataList = excelImport.getDataList(MaterialImportVo.class);
        } catch (Exception e) {
            log.error("获取数据信息失败!");
            throw new ExcelException("获取数据信息失败!");
        }
        if (ListUtils.isEmpty(dataList)) {
            throw new ExcelException("导入数据不能为空！");
        }
        Spu newspu = new Spu();
        List<String> scaSpuNos = new ArrayList<String>();
        int line = 0;
        for (MaterialImportVo data : dataList) {
            data = this.removeSpecialChar(data);
            line++;
            String addSpuYn = "Y";
            String custGoodNo = data.getCsaSpuCode();
            String category = "";
            if(!StringUtils.isNull(data.getCategoryName())){
                category =data.getCategoryName();
                if(!StringUtils.isNull(data.getCategoryNameTwo())){
                    category =category.concat("-").concat(data.getCategoryNameTwo());
                    if(!StringUtils.isNull(data.getCategoryNameThree())){
                        category =category.concat("-").concat(data.getCategoryNameThree());

                    }
                }
            }
            data.setCategoryName(category);

            if(StringUtils.isNull(custGoodNo)){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
                operationLog.setErrorMsg("客户商品编码为空");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);//add log
                logNum++;
                continue;
            }else{
                custGoodNo =this.removeSpecialChar(custGoodNo);
            }
            if(StringUtils.isNull(data.getCustRelSalesPrice())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
                operationLog.setErrorMsg("相应得销售费率没有提供");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);//add log
                logNum++;
                continue;
            }else{
                String rt =  this.removeSpecialChar(data.getCustRelSalesPrice());
                data.setCustRelSalesPrice(rt);
            }
            if(StringUtils.isNull(data.getSettlePrice())){
               /* OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
                operationLog.setErrorMsg("采购费率没有提供");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
                logNum++;
                continue;*/
                data.setSettlePrice("0.0");
            }else{
                String rt =  this.removeSpecialChar(data.getSettlePrice());
                data.setSettlePrice(rt);
               /* if(!StringUtils.isNumeric(data.getSettlePrice())){
                    //add log
                    OperationLog operationLog =new OperationLog();
                    operationLog.setOperationType(ErrorType.SPUIMPORT.getStatus());
                    operationLog.setErrorMsg("采购费率数字格式不对");
                    operationLog.setRequestMethod(custGoodNo);
                    operationLog.setMethod("importData");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);
                    logNum++;
                    continue;
                }*/
            }


            StoreVo storeVo = null;
            String supplierNo ="";
            String supplierName ="";
            if(StringUtils.isNull(data.getStoreCode())){
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
                operationLog.setErrorMsg("店铺信息没有输入");
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
                logNum++;
                continue;
            }else{
                String rt =  this.removeSpecialChar(data.getStoreCode());
                data.setStoreCode(rt);

                storeVo= iStoreService.findByCode(data.getStoreCode());
                if(storeVo==null){
                    OperationLog operationLog =new OperationLog();
                    operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
                    operationLog.setErrorMsg(data.getStoreCode()+"店铺基础数据没有维护");
                    operationLog.setRequestMethod(custGoodNo);
                    operationLog.setLocation("第"+line+"行");
                    operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
                    operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                    if (user != null) {
                        operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                    }
                    operationLogMapper.insert(operationLog);
                    logNum++;
                    continue;
                }
                if(StringUtils.isNull(mallId)){
                    mallId =storeVo.getMallId();
                }
                supplierNo = storeVo.getSupplierId();
                supplierName = storeVo.getSupplierName();
            }
            if(scaSpuNos.contains(custGoodNo)){
                List<Spu> spus =  this.spuMapper.findByCustSpuNo(custGoodNo,mallId);
                if(spus!=null&&spus.size()>0){
                    newspu =(Spu)spus.get(0);
                    addSpuYn = "N";
                }

            }else{
                scaSpuNos.add(custGoodNo);
                addSpuYn = "Y";
            }
            // add more data check log
            try {
                MaterialSalesPriceSaveParam saleParam = new MaterialSalesPriceSaveParam();
                MaterialBuyPriceSaveParam saveParam = new MaterialBuyPriceSaveParam();

                BeanUtils.copyProperties(data, saveParam);
                if(StringUtils.isNull(data.getSettlePrice())){
                    saveParam.setSettlePrice(new BigDecimal(0.0));
                }
                if(StringUtils.isNull(data.getCustGoodsNo())){ // 导入数据没有SKU
                    CustGoodsMapVo custGoodsMapVo = custGoodsMapMapper.findByCustGoodNoAndSkuNoForInit(custGoodNo,"通用",mallId);
                    if(custGoodsMapVo==null){// 映射表木有
                        CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setMallId(mallId);
                        custGoodsMapSaveParam.setInitFlag("Y");
                        custGoodsMapSaveParam.setCategoryName(data.getCategoryName());
                        custGoodsMapSaveParam.setBrandName(data.getBrandName());
                        custGoodsMapSaveParam.setBarCode(data.getBarCode());
                        custGoodsMapSaveParam.setSn(data.getCustGoodsNo());
                     /*   if(data.getGroupOrSpu().equals("组合")) {
                            custGoodsMapSaveParam.setGoodsType("gp");
                            GoodsGroup newGoodsGroup = this.generateNewGoodsGroup(data);
                            custGoodsMapSaveParam.setSpuId(newGoodsGroup.getId());
                            custGoodsMapSaveParam.setGroupId(newGoodsGroup.getId());
                            this.generateNewGoodsSupplier(data,supplierNo,supplierName,newGoodsGroup.getId());

                            saveParam.setGroupOrSpu("gp");
                            saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saveParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode("通用");
                            saleParam.setGroupOrSpu("gp");
                            saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saleParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saleParam.setCustomerNo(storeVo.getMallId());
                        }else{*/

                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(data,storeVo,custGoodNo);
                        }

                        // Sku newsku = this.generateNewSku(data,newspu);
                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        custGoodsMapSaveParam.setCustSkuNo("通用");
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setBrandId(newspu.getBrandId());
                        custGoodsMapSaveParam.setStoreId(storeVo.getId());
                        custGoodsMapSaveParam.setSpuName(newspu.getSpuName());
                        //  String cgmid =iCustGoodsMapService.insert(custGoodsMapSaveParam);
                        GoodsSupplier goodsSupplier =  this.generateNewGoodsSupplier(data,supplierNo,supplierName,newspu.getId());
                        saveParam.setCsaSpuCode(data.getCsaSpuCode());
                        saveParam.setGroupOrSpu("spu");
                        saveParam.setBarCode(data.getBarCode());
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);
                        saveParam.setCsaSkuCode("通用");
                        saleParam.setGroupOrSpu("spu");
                        saveParam.setBrandName(custGoodsMapSaveParam.getBrandName());
                        saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saleParam.setCustomerNo(storeVo.getMallId());
                        saleParam.setSkuId(custGoodsMapSaveParam.getSkuId());

                        // }
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());
                        saleParam.setMaterialMapId(entity.getId());
                    }else{ //y映射表里有
                        continue;
                     /*   GoodsSupplierVo goodsSupplierVo=
                                goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,custGoodsMapVo.getSpuId());
                        if(goodsSupplierVo==null){
                            GoodsSupplier goodsSupplierSaveParam=  this.generateNewGoodsSupplier(data,supplierNo,supplierName,custGoodsMapVo.getSpuId());
                       */   //  if(data.getGroupOrSpu().equals("单品")){
                         /*       saveParam.setGroupOrSpu("spu");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());
                                saleParam.setGroupOrSpu("spu");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());*/

                         /*   }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setGroupOrSpu("gp");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());

                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                        //  saveParam.setMaterialMapId(custGoodsMapVo.getId());
                        //   saleParam.setMaterialMapId(custGoodsMapVo.getId());
                        //  }else{
                        //      saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                        //    if(data.getGroupOrSpu().equals("单品")){
                         /*       saveParam.setGroupOrSpu("spu");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGroupOrSpu("spu");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());*/

                          /*  }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGroupOrSpu("gp");
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                        //    saveParam.setMaterialMapId(custGoodsMapVo.getId());
                        //    saleParam.setMaterialMapId(custGoodsMapVo.getId());
                        //  }
                    }

                }else{ //导入数据带SKU
                    CustGoodsMapVo custGoodsMapVo = custGoodsMapMapper.findByCustGoodNoAndSkuNoForInit(custGoodNo,data.getCustGoodsNo(),mallId);
                    if(custGoodsMapVo==null){
                        CustGoodsMapSaveParam custGoodsMapSaveParam = new CustGoodsMapSaveParam();
                        custGoodsMapSaveParam.setCustGoodsId(custGoodNo);
                        custGoodsMapSaveParam.setMallId(mallId);
                        custGoodsMapSaveParam.setInitFlag("Y");
                        custGoodsMapSaveParam.setCustSkuNo(data.getCustGoodsNo());
                        custGoodsMapSaveParam.setCategoryName(data.getCategoryName());
                        custGoodsMapSaveParam.setBrandName(data.getBrandName());
                        custGoodsMapSaveParam.setBarCode(data.getBarCode());
                        custGoodsMapSaveParam.setSn(data.getCustGoodsNo());
                      /*  if(data.getGroupOrSpu().equals("组合")) {
                            custGoodsMapSaveParam.setGoodsType("gp");
                            GoodsGroup newGoodsGroup = this.generateNewGoodsGroup(data);
                            custGoodsMapSaveParam.setSpuId(newGoodsGroup.getId());
                            custGoodsMapSaveParam.setGroupId(newGoodsGroup.getId());
                            this.generateNewGoodsSupplier(data,supplierNo,supplierName,newGoodsGroup.getId());

                            saveParam.setGroupOrSpu("gp");
                            saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saveParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saveParam.setSupplierNo(supplierNo);
                            saveParam.setCsaSpuCode(data.getCsaSpuCode());
                            saveParam.setCsaSkuCode(data.getCustGoodsNo());

                            saleParam.setGroupOrSpu("gp");
                            saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                            saleParam.setGoodGroupId(custGoodsMapSaveParam.getGroupId());
                            saleParam.setCustomerNo(storeVo.getMallId());
                        }else{*/
                        custGoodsMapSaveParam.setGoodsType("sku");
                        custGoodsMapSaveParam.setCustSkuNo(data.getCustGoodsNo());
                        if(addSpuYn.equals("Y")){
                            newspu = this.generateNewSpu(data,storeVo,custGoodNo);
                        }
                        Sku newsku = this.generateNewSku(data,newspu);
                        custGoodsMapSaveParam.setBrandId(newspu.getBrandId());
                        custGoodsMapSaveParam.setStoreId(storeVo.getId());
                        custGoodsMapSaveParam.setSkuId(newsku.getId());
                        custGoodsMapSaveParam.setSpuId(newspu.getId());
                        custGoodsMapSaveParam.setSpuName(newspu.getSpuName());
                        this.generateNewGoodsSupplier(data,supplierNo,supplierName,newspu.getId());
                        saveParam.setGroupOrSpu("sku");
                        saveParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saveParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saveParam.setSupplierNo(supplierNo);
                        saveParam.setCsaSpuCode(data.getCsaSpuCode());
                        saveParam.setCsaSkuCode(data.getCustGoodsNo());
                        saveParam.setBrandName(custGoodsMapSaveParam.getBrandName());
                        saveParam.setBarCode(data.getBarCode());
                        saleParam.setGroupOrSpu("sku");
                        saleParam.setSpuId(custGoodsMapSaveParam.getSpuId());
                        saleParam.setSkuId(custGoodsMapSaveParam.getSkuId());
                        saleParam.setCustomerNo(storeVo.getMallId());
                        // }
                        CustGoodsMap entity = new CustGoodsMap();
                        BeanUtils.copyProperties(custGoodsMapSaveParam, entity);
                        String mapid =   this.generalSeqService.generateSeq("custMap");

                        entity.setId(mapid + "").preInsert(user.getId());
                        custGoodsMapMapper.insert(entity);
                        saveParam.setMaterialMapId(entity.getId());
                        saleParam.setMaterialMapId(entity.getId());
                    }else{ // 映射表有数据
                        continue;
                        //     GoodsSupplierVo goodsSupplierVo=
                        //             goodsSupplierMapper.findBySupplierNoAndGoodsNo(supplierNo,custGoodsMapVo.getSpuId());
                        //     if(goodsSupplierVo==null){
                        //         GoodsSupplier goodsSupplierSaveParam=  this.generateNewGoodsSupplier(data,supplierNo,supplierName,custGoodsMapVo.getSpuId());
                        //  if(data.getGroupOrSpu().equals("单品")){
                     /*           saveParam.setGroupOrSpu("spu");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saleParam.setGroupOrSpu("spu");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());*/

                          /*  }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setGroupOrSpu("gp");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());

                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                        //   saveParam.setMaterialMapId(custGoodsMapVo.getId());
                        //  saleParam.setMaterialMapId(custGoodsMapVo.getId());
                        //   }else{
                        //      saveParam.setSupplierNo(goodsSupplierVo.getSupplierId());
                        //  if(data.getGroupOrSpu().equals("单品")){
                        /*        saveParam.setGroupOrSpu("spu");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGroupOrSpu("spu");
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());*/

                          /*  }else if(data.getGroupOrSpu().equals("组合")){
                                saveParam.setGroupOrSpu("gp");
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setGroupOrSpu("gp");
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setGoodGroupId(custGoodsMapVo.getGroupId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }else{
                                saveParam.setGroupOrSpu("sku");
                                saveParam.setSkuId(custGoodsMapVo.getSkuId());
                                saveParam.setSpuId(custGoodsMapVo.getSpuId());
                                saveParam.setSupplierNo(supplierNo);
                                saveParam.setCsaSpuCode(data.getCsaSpuCode());
                                saveParam.setCsaSkuCode(custGoodsMapVo.getCustSkuNo());

                                saleParam.setGroupOrSpu("sku");
                                saleParam.setSkuId(custGoodsMapVo.getSkuId());
                                saleParam.setSpuId(custGoodsMapVo.getSpuId());
                                saleParam.setCustomerNo(storeVo.getMallId());
                            }*/
                        //  saveParam.setMaterialMapId(custGoodsMapVo.getId());
                        //   saleParam.setMaterialMapId(custGoodsMapVo.getId());
                        //  }
                    } // end else map null
                } // end has sku



                saleParam.setSettlePrice(new BigDecimal(Double.valueOf(data.getCustRelSalesPrice())));
                if(!StringUtils.isNull(data.getCustMileSalesPrice())){
                    saleParam.setCustMileSalesPrice(Double.valueOf(data.getCustMileSalesPrice()));
                    saveParam.setCustMileSalesPrice(Double.valueOf(data.getCustMileSalesPrice()));
                }
                if(!StringUtils.isNull(data.getCustCashSalesPrice())){
                   saveParam.setCustCashSalesPrice(Double.valueOf(data.getCustCashSalesPrice()));
                }
                saleParam.setPriceDefType("$");
                Date applyDate = DateUtils.parseDate("2021-01-01");
                saveParam.setApplyDate(applyDate);
                saleParam.setApplyDate(applyDate);
                saleParam.setStoreId(storeVo.getId());
                saveParam.setStoreId(storeVo.getId());
                saveParam.setMallCustNo(storeVo.getMallId());
                saveParam.setBarCode(data.getBarCode());
                saveParam.setSettlePrice(new BigDecimal(Double.valueOf(data.getSettlePrice())));
                if(data.getWholeSaleQuantity()==null){
                    saveParam.setWholeSaleQuantity(new Long(0));
                }
                saveParam.setGroupOrSpu(data.getGroupOrSpu());
                saveParam.setCustGoodsName(data.getCustGoodsName());

                this.insert(saveParam);
                iMaterialSalesPriceService.insert(saleParam);
                successNum++;
                successMsg.append("<br/>" + successNum + "导入成功");
            } catch (Exception e) {
                failureNum++;
                logNum++;
                String msg = "<br/>" + failureNum + "导入失败：";
                failureMsg.append(msg + e.getMessage());
                //add log table
                OperationLog operationLog =new OperationLog();
                operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
                operationLog.setErrorMsg(failureMsg.toString());
                operationLog.setRequestMethod(custGoodNo);
                operationLog.setLocation("第"+line+"行");
                operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
                operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
                if (user != null) {
                    operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
                }
                operationLogMapper.insert(operationLog);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            OperationLog operationLog =new OperationLog();
            operationLog.setOperationType(ErrorType.SPUIMPORT.getStatusName());
            operationLog.setErrorMsg(failureMsg.toString());
            operationLog.setLocation("第"+line+"行");
            operationLog.setMethod("导入模板-商城商品信息和价格-初始化数据");
            operationLog.setId(idWorker.nextId() + "").preInsert(user.getId());
            if (user != null) {
                operationLog.setOperatorName(user.getUserName() + "(" + user.getNickName() + ")");
            }
            operationLogMapper.insert(operationLog);
            throw new ExcelException(failureMsg.toString());
        } else {
            if(logNum>0){
                successMsg.insert(0, "恭喜您，操作成功！共 " + successNum + " 条，另有：" +logNum+ " 条数据不能正常导入，已写入日志");

            }else{
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");

            }
        }
        return successMsg.toString();
    }
    @Override
    public String removeSpecialChar(String input){
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(input);
        String rt = m.replaceAll("");
        return rt;
    }

    @Override
    public MaterialBuyPriceVo removeSpecialCharMaterialBuyPriceVo(MaterialBuyPriceVo input){

        Pattern p =null;
        Matcher m = null;;
        String rt = "";
        if(StringUtils.isNull(input.getGroupOrSpu())){
            input.setGroupOrSpu("单品");
        }else{
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getGroupOrSpu());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setGroupOrSpu(rt);
        }
        if(!StringUtils.isNull(input.getCsaSpuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSpuCode());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCsaSpuCode(rt);
        }
        if(!StringUtils.isNull(input.getCustGoodsNo())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsNo());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCustGoodsNo(rt);
        }

        if(!StringUtils.isNull(input.getCustGoodsName())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsName());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCustGoodsName(rt);
        }

        if(!StringUtils.isNull(input.getCustMileSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustMileSalesPrice());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCustMileSalesPrice(rt);
        }

        if(!StringUtils.isNull(input.getCustRelSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustRelSalesPrice());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCustRelSalesPrice(rt);
        }

        if(!StringUtils.isNull(input.getSettlePrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getSettlePrice());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setSettlePrice(rt);
        }
        if(!StringUtils.isNull(input.getCsaSkuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSkuCode());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCsaSkuCode(rt);
        }

        return input;
    }
    @Override
    public MaterialImportVo removeSpecialChar(MaterialImportVo input){
        Pattern p =null;
        Matcher m = null;;
        String rt = "";
        if(StringUtils.isNull(input.getGroupOrSpu())){
            input.setGroupOrSpu("单品");
        }else{
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getGroupOrSpu());
            rt = m.replaceAll("");
            input.setGroupOrSpu(rt);
        }
        if(!StringUtils.isNull(input.getCsaSpuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCsaSpuCode());
            rt = m.replaceAll("");
            input.setCsaSpuCode(rt);
        }
        if(!StringUtils.isNull(input.getCustGoodsNo())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsNo());
            rt = m.replaceAll("");
            input.setCustGoodsNo(rt);
        }

        if(!StringUtils.isNull(input.getCustGoodsName())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustGoodsName());
            rt = m.replaceAll("");
            input.setCustGoodsName(rt);
        }

        if(!StringUtils.isNull(input.getCustMileSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustMileSalesPrice());
            rt = m.replaceAll("");
            input.setCustMileSalesPrice(rt);
        }

        if(!StringUtils.isNull(input.getCustRelSalesPrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustRelSalesPrice());
            rt = m.replaceAll("");
            input.setCustRelSalesPrice(rt);
        }

        if(!StringUtils.isNull(input.getSettlePrice())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getSettlePrice());
            rt = m.replaceAll("");
            input.setSettlePrice(rt);
        }
        if(!StringUtils.isNull(input.getStoreCode())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getStoreCode());
            rt = m.replaceAll("");
            input.setStoreCode(rt);
        }

        return input;
    }
    /**
     * 生成商品组合
     */
    @Override
    public GoodsGroup generateNewGoodsGroup(MaterialImportVo materialBuyPriceVo){
        GoodsGroup spu = new GoodsGroup();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setGoodsGroupName(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuName(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuId(materialBuyPriceVo.getSpuId());
        spu.setId(idWorker.nextId() + "").preInsert(user.getId());
        goodsGroupMapper.insert(spu);
        return spu;
    }
    /**
     * 生成供应商商品链表
     */
    @Override
    public GoodsSupplier generateNewGoodsSupplier(MaterialImportVo materialBuyPriceVo,String supplierNo,String supplierName,String spuid){
        GoodsSupplier spu = new GoodsSupplier();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        spu.setSupplierId(supplierNo);
        if(materialBuyPriceVo.getGroupOrSpu().equals("单品")){
            spu.setGoodsType("spu");
        }else if(materialBuyPriceVo.getGroupOrSpu().equals("组合")){
            spu.setGoodsType("gp");
        }else{
            spu.setGoodsType("sku");
        }
        spu.setSpuId(spuid);
        spu.setSupplierName(supplierName);
        spu.setWarehouse(materialBuyPriceVo.getWarehouse());
        spu.setId(idWorker.nextId() + "").preInsert(user.getId());
        goodsSupplierMapper.insert(spu);
        return spu;
    }

    /**
     * 生成商品SKU
     */
    @Override
    public Sku generateNewSku(MaterialImportVo materialBuyPriceVo,Spu spu){
        Sku sku = new Sku();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        sku.setBrandId(materialBuyPriceVo.getBrandId());
        sku.setBrandName(materialBuyPriceVo.getBrandName());
        sku.setCategoryId(materialBuyPriceVo.getCategoryId());
        sku.setSpuId(spu.getId());
        // sku.setSn(materialBuyPriceVo.)
        sku.setCustSkuId(materialBuyPriceVo.getCustGoodsNo());
        sku.setCategoryName(materialBuyPriceVo.getCategoryName());
        sku.setImage(materialBuyPriceVo.getImage());
        sku.setSpec(materialBuyPriceVo.getMaterialGuige());
        sku.setSkuName(materialBuyPriceVo.getCustGoodsName());
        sku.setStatus(SpuStatusEnum.CREATED.getStatus());
        String skuid =   this.generalSeqService.generateSeq("sku");
        sku.setId(skuid);
        sku.preInsert(user.getId());

        skuMapper.insert(sku);
        return sku;
    }
    /**
     * 生成商品
     */
    @Override
    public Spu generateNewSpu(MaterialImportVo materialBuyPriceVo,StoreVo storeVo,String custSpuNo){
        Spu spu = new Spu();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        if(!StringUtils.isNull(materialBuyPriceVo.getBrandId())){
            spu.setBrandId(materialBuyPriceVo.getBrandId());
            spu.setBrandName(materialBuyPriceVo.getBrandName());
        }else if(!StringUtils.isNull(materialBuyPriceVo.getBrandName())){
            Brand brand = brandMapper.findByName(materialBuyPriceVo.getBrandName());
            if(brand==null){
                brand = new Brand();
                brand.setBrandCode(materialBuyPriceVo.getBrandName());
                brand.setBrandName(materialBuyPriceVo.getBrandName());
                brand.setStoreId(storeVo.getId());
                brand.setId(idWorker.nextId() + "").preInsert(user.getId());
                brandMapper.insert(brand);
                spu.setBrandId(brand.getId());
                spu.setBrandName(materialBuyPriceVo.getBrandName());
            }else{
                spu.setBrandId(brand.getId());
                spu.setBrandName(materialBuyPriceVo.getBrandName());
            }


        }
        spu.setCategoryId(materialBuyPriceVo.getCategoryId());
        spu.setTitle(materialBuyPriceVo.getCustGoodsName());
        spu.setCategoryName(materialBuyPriceVo.getCategoryName());
        spu.setImage(materialBuyPriceVo.getImage());
        spu.setIntroduction(materialBuyPriceVo.getCustGoodsName());
        spu.setSpuName(materialBuyPriceVo.getCustGoodsName());
        spu.setStatus(SpuStatusEnum.CREATED.getStatus());
        spu.setCustSpuNo(custSpuNo);
        spu.setSpecs(materialBuyPriceVo.getMaterialGuige());
        String spuid =   this.generalSeqService.generateSeq("spu");
        spu.setId(spuid);
        spu.preInsert(user.getId());
        spu.setSite(storeVo.getMallId());
        spuMapper.insert(spu);
        return spu;
    }
}
